package com.sskj.market.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;
import com.sskj.market.bean.OrderListBean;
import com.sskj.market.ui.activity.OrderItemFragment;

import java.util.List;

import io.reactivex.Flowable;

/**
 * @author :budou
 * Package :com.sskj.market.ui.activity
 * Date :2019/7/25 16:16
 * Describe :订单列表presenter
 * Remark :
 */
public class OrderItemPresenter extends BasePresenter<OrderItemFragment> {

    public Flowable<List<OrderListBean>> getAllList(String page, int type) {
        if (type == 0) {
            return httpService.getOrderList(page)
                    .converter(new JsonConverter<HttpData<PageBean<OrderListBean>>>() {
                    })
                    .adapt(new FlowableBody<>())
                    .map(pageBeanHttpData -> pageBeanHttpData.getData().getList());
        } else {
            return httpService.getStatusList(page, type + "")
                    .converter(new JsonConverter<HttpData<PageBean<OrderListBean>>>() {
                    })
                    .adapt(new FlowableBody<>())
                    .map(pageBeanHttpData -> pageBeanHttpData.getData().getList());
        }

    }

    public void cancelOrder(String orderId) {
        httpService.orderCancel(orderId)
                .execute(new JsonCallBack<HttpData>(true) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        mView.cancelSuccess();
                    }
                });
    }

    public void deleteOrder(String orderId) {
        httpService.orderDelete(orderId)
                .execute(new JsonCallBack<HttpData>(true) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        mView.deleteSuccess();
                    }
                });

    }

    public void ensureOrder(String orderId) {
        httpService.orderEnsure(orderId).execute(new JsonCallBack<HttpData>() {
            @Override
            public void onSuccess(Response<HttpData> response) {
                mView.ensureSuccess();
            }
        });
    }

    public void payOrder(String sn, String pwd) {
        httpService.orderPay(sn, pwd).execute(new JsonCallBack<HttpData>(true) {
            @Override
            public void onSuccess(Response<HttpData> response) {
                mView.paySuccess();
            }

        });
    }

}
